<template>
    <el-dialog
        v-dialogDrag
        :title="dialogType == 'CORP' ? '企业申请印章' : '个人申请印章'"
        :close-on-click-modal="false"
        :visible.sync="visible"
        @close="handleClose"
        :center="true"
    >
        <div v-if="dialogType == 1">
            <el-form :model="addDataForm" ref="addDataForm" label-width="80px">
                <el-form-item label="公司名称" prop="userName">
                    <el-input v-model="addDataForm.userName" placeholder="请输入公司名称"  clearable></el-input>
                </el-form-item>

                <el-form-item label="单位类型">
                    <el-select v-model="corpTypeName" placeholder="请选择" style="width: 100%" :disabled="corpTypeSelectedDis">
                        <el-option
                            v-for="(item, index) in corpTypeOptions"
                            :key="index"
                            :label="item.corpTypeName"
                            :value="item.corpTypeId"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="企业注册类型">
                    <el-select
                        v-model="corpRegisterTypeName"
                        placeholder="请选择"
                        style="width: 100%"
                        :disabled="corpRegisterTypeSelectedDis"
                    >
                        <el-option
                            v-for="(item, index) in corpRegisterTypeOptions"
                            :key="index"
                            :label="item.corpRegisterTypeName"
                            :value="item.corpRegisterTypeId"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="代码补充信息" prop="certificateId">
                    <el-input v-model="addDataForm.certificateId" placeholder="请输入代码补充信息"  clearable></el-input>
                </el-form-item>
            </el-form>
        </div>

        <div v-else>
            <el-form :model="dataForm"  ref="dataForm" label-width="80px">
                <el-form-item label="姓名" prop="realName">
                    <el-input v-model="dataForm.realName" placeholder="请输入姓名"  clearable></el-input>
                </el-form-item>

                <el-form-item label="身份证号" prop="cardId">
                    <el-input type="text" v-model="dataForm.cardId" placeholder="请输入联系人身份证"  clearable></el-input>
                </el-form-item>
            </el-form>
        </div>

        <span slot="footer" class="dialog-footer">
            <el-button @click="handleClose">取消</el-button>
            <el-button type="primary" @click="addDataFormSubmit()">申请</el-button>
        </span>
    </el-dialog>
</template>
<script>
import API from '@/api';
import cloneDeep from 'lodash/cloneDeep';
export default {
    data() {
        return {
            visible: false,
            dialogType: '',

            //-- 企业
            addDataForm: {
                userName: '', //公司名称
                certificateId: '' //营业执照代码
            },

            //单位类型 ：  0-普通企业，1-社会团体，2-事业单位，3-民办非企业单位，4-党政及国家机构
            corpTypeName: '',
            corpTypeSelectedDis: false,
            corpTypeOptions: [
                { corpTypeId: '0', corpTypeName: '普通企业', selected: true },
                { corpTypeId: '1', corpTypeName: '社会团体', selected: false },
                { corpTypeId: '2', corpTypeName: '事业单位', selected: false },
                { corpTypeId: '3', corpTypeName: '民办非企业单位', selected: false },
                { corpTypeId: '4', corpTypeName: '党政及国家机构', selected: false }
            ],

            //企业注册类型：NORMAL:组织机构代码号，MERGE：多证合一，传递社会信用代码号,REGCODE:企业工商注册码
            corpRegisterTypeName: '',
            corpRegisterTypeSelectedDis: false,
            corpRegisterTypeOptions: [
                { corpRegisterTypeId: 'NORMAL', corpRegisterTypeName: '组织机构代码号', selected: true },
                { corpRegisterTypeId: 'MERGE', corpRegisterTypeName: '多证合一，传递社会信用代码号', selected: false },
                { corpRegisterTypeId: 'REGCODE', corpRegisterTypeName: '企业工商注册码', selected: false }
            ],

            //-- 个人
            dataForm: {
                realName: '', //姓名
                cardId: '' //身份证号
            }
        };
    },

    methods: {
        handleClose() {
            this.addDataForm = {};
            this.dataForm = {};
            this.visible = false;
        },

        //初始化
        init(type) {
            this.visible = true;
            this.dialogType = type;
        },

        //获取企业注册类型
        getCorpRegisterTypeNameptions(parm, name) {
            if (this.isChinese(name)) {
                //全是中文，代表用户没有选择，为默认值，或者
                for (var i = 0; i < this.corpRegisterTypeOptions.length; i++) {
                    if (this.corpRegisterTypeOptions[i].corpRegisterTypeName === name) {
                        this.addDataForm.corpRegisterType = this.corpRegisterTypeOptions[i].corpRegisterTypeId;
                        parm.corpRegisterType = this.corpRegisterTypeOptions[i].corpRegisterTypeId;
                    }
                }
            } else {
                //选择之后，name为代码id
                for (var i = 0; i < this.corpRegisterTypeOptions.length; i++) {
                    if (this.corpRegisterTypeOptions[i].corpRegisterTypeId === name) {
                        this.addDataForm.corpRegisterType = this.corpRegisterTypeOptions[i].corpRegisterTypeId;
                        parm.corpRegisterType = this.corpRegisterTypeOptions[i].corpRegisterTypeId;
                    }
                }
            }
        },

        //获取单位类型
        getcorpTypeNameOptions(parm, name) {
            if (this.isChinese(name)) {
                //全是中文，代表用户没有选择，为默认值，或者
                for (var i = 0; i < this.corpTypeOptions.length; i++) {
                    if (this.corpTypeOptions[i].corpTypeName === name) {
                        this.addDataForm.corpType = this.corpTypeOptions[i].corpTypeId;
                        parm.corpType = this.corpTypeOptions[i].corpTypeId;
                    }
                }
            } else {
                //选择之后，name为代码id
                for (var i = 0; i < this.corpTypeOptions.length; i++) {
                    if (this.corpTypeOptions[i].corpTypeId === name) {
                        this.addDataForm.corpType = this.corpTypeOptions[i].corpTypeId;
                        parm.corpType = this.corpTypeOptions[i].corpTypeId;
                    }
                }
            }
        },

        //是否全是 中文
        isChinese(temp) {
            var re = /[^\u4e00-\u9fa5]/;
            if (re.test(temp)) return false;
            return true;
        },

        // 表单提交
        addDataFormSubmit() {
            if (this.dialogType === 'CORP') {
                //公司

                let params = cloneDeep(this.addDataForm);
                //处理单位类型选择
                this.getcorpTypeNameOptions(params, this.corpTypeName);

                //处理企业注册类型
                this.getCorpRegisterTypeNameptions(params, this.corpRegisterTypeName);

                var tick = API.transactionManager.getCqjyOrderDetails_addCorpAccount(params);
                
            }

            if (this.dialogType === 'PERSON') {
                //个人
                let params = cloneDeep(this.dataForm);
                var tick = API.transactionManager.getCqjyOrderDetails_addPersonAccount(params);
            }

            tick.then((data) => {
                    if (data.code == '0000') {
                        this.$message({
                            message: `申请成功`,
                            type: 'success',
                            duration: 1500,
                            onClose: () => {
                                this.visible = false;
                                this.$emit('refreshDataList');
                            }
                        });
                    }
                });
        }
    }
};
</script>